using System;
using System.Runtime.CompilerServices;
using System.Collections.Generic;
using System.IO;
using System.Collections.Specialized;
using System.Diagnostics;
using System.Net;
using System.Text;
using System.Threading;

namespace CStock
{
   

    public class WebClientFeed: StockObjectBase
    {
        protected static bool webClientBusy;
        protected static System.Net.WebClient webclient;
        
        
        protected IDataItem dataItem;
        protected DateTime dateStart;
        protected string url;


        public WebClientFeed()
        {
            webclient = new System.Net.WebClient();
            
            webClientBusy = false;

        }
        public static void DownloadFile(string url, string path)
        {

            try
            {
                System.Net.WebClient client = new WebClient();
                client.DownloadFile(url, path);
            }
            catch (Exception err)
            {
                Console.WriteLine(err.ToString());
            }


        }
        private static void CheckClient()
        {
            if (webclient == null)
                webclient = new WebClient();
        }
        public static string GetInternetPage(string myURL)
        {
            CheckClient();

            string webSite = string.Empty;
            webClientBusy = true;
            try
            {
                webSite = webclient.DownloadString(myURL);
                
            }
            catch(Exception err)
            {
                Debug.Assert(false, "Error reading internet page : " + myURL + ": " + err.Message);
                return null;
            }
            webClientBusy = false;
            return webSite;


        }
        public static IList<string> ReadPage_ToList(string myURL)
        {
            StreamReader sr = ReadPageToStreamReader(myURL);

            if (sr != null)
            {
                IList<string> list = new List<string>();
                string line;
                // Read and display lines from the file until the end of 
                // the file is reached.
                while ((line = sr.ReadLine()) != null)
                {
                    list.Add(line);
                }

                webClientBusy = false;
                return list;
            }

            return null;



        }
        private static StreamReader ReadPageToStreamReader(string myURL)
        {
            CheckClient();

            webClientBusy = true;
            
            System.IO.Stream stream = null;
            try
            {
                //webclient.Headers
                stream = webclient.OpenRead(myURL);
            }
            catch(Exception err)
            {
                System.Windows.Forms.MessageBox.Show("Could not connect to internet - PLEASE open the page in the browser: " + myURL);
                Console.WriteLine("Error downloading internet page : " + myURL + " ; Error is : " + err.Message);
                //Debug.Assert(false, "Error downloading internet page : " + myURL + " ; Error is : " + err.Message);
                return null;
            }
            
            if (stream == null)
            {
                Debug.WriteLine("Have to wait - web client busy");
                webClientBusy = false;
                System.Windows.Forms.MessageBox.Show("Error downloading stream for : " + myURL);
                return null;

            }

            StreamReader sr = new StreamReader(stream);
            return sr;
        }
        public static StringCollection ReadPage_ToStringCollection(string myURL)
        {
            StreamReader sr = ReadPageToStreamReader(myURL);

            if (sr != null)
            {
                System.Collections.Specialized.StringCollection strCollection = new System.Collections.Specialized.StringCollection();
                string line;
                // Read and display lines from the file until the end of 
                // the file is reached.
                while ((line = sr.ReadLine()) != null)
                {
                    strCollection.Add(line);
                }

                webClientBusy = false;
                return strCollection;
            }
            return null;
        }

        
    }
}